const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
var areasInfo = require('../../utils/area.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    desk: [],
    tips: "提示:选择房间或餐台后请点击锁定!",
    value: "",
    uuid: "",
    show: false,
    userName: '',
    phone: "",
    address: '',
    checked: false,
    btnType: 0,
    radio: "1",
    id: '',
    relationList: [],
    citySelectShow: false,
    areaList: [],
    province: '',
    city: '',
    area: '',
    longitude: '',
    latitude: ''
  },
  onRadioChange: function(event) {
    console.log(event);
    let _that = this;
    this.setData({
      radio: event.detail
    });
  },
  onChange: function(event) {
    this.setData({
      checked: event.detail
    });
  },
  insertOrUpdateRelation: function(option) {
    let _that = this;
    let userName = _that.data.userName;
    let phone = _that.data.phone;
    let address = _that.data.address;
    let checked = _that.data.checked;
    let defaultStatus = checked ? 1 : 0;
    let method = option.currentTarget.dataset.method;
    let uuid = _that.data.uuid;
    let longitude = _that.data.longitude;
    let latitude = _that.data.latitude;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/CommonRelationApi/insertCommonRelation';
      tempData = {
        name: userName,
        phone: phone,
        defaultStatus: defaultStatus,
        uuid: uuid,
        address: address,
        longitude: longitude,
        latitude: latitude
      }
    } else {
      tempUrl = url + '/CommonRelationApi/updateCommonRelationById';
      let id = _that.data.id;
      tempData = {
        id: id,
        name: userName,
        phone: phone,
        defaultStatus: defaultStatus,
        uuid: uuid,
        address: address,
        longitude: longitude,
        latitude: latitude
      }
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(uuid);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        }else if (res.data.code == 500) {
          // 尚未登录
          Dialog.confirm({
            title: '提示',
            message: '您当前尚未登录, 请点击确认跳转登录页面!'
          }).then(() => {
            // on confirm
            wx.navigateTo({
              url: "../../pages/login/index"
            })
          }).catch(() => {
            // on cancel
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  /**表单 start */
  onUserNameChange: function(option) {
    let _that = this;
    _that.setData({
      userName: option.detail
    })
  },
  /***
   * onShow
   */
  onShow: function(){
    let _that = this;
    wx.getStorage({
      key: "openId",
      success(res) {
        console.log(res);
        _that.setData({
          uuid: res.data
        })
        _that.queryData(res.data);
      },
      fail(res){
        // 尚未登录。弹出登录框
        Dialog.confirm({
          title: '提示',
          message: '您当前尚未登录, 请点击确认跳转登录页面!'
        }).then(() => {
          // on confirm
          wx.navigateTo({
            url: "../../pages/login/index"
          })
        }).catch(() => {
          // on cancel
        });
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  onPhoneChange: function(option) {
    let _that = this;
    _that.setData({
      phone: option.detail
    })
  },
  onAddressChange: function(option) {
    let _that = this;
    _that.setData({
      address: option.detail
    })

  },
  mapViewTap() {
    var th = this;
    var longitude = th.data.longitude;
    var latitude = th.data.latitude;
    if (longitude != '') {
      wx.openLocation({
        latitude: Number(latitude),
        longitude: Number(longitude),
        scale: 17
      })
    } else {

      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          console.log(res)
          wx.openLocation({
            latitude: res.latitude,
            longitude: res.longitude,
            scale: 17
          })
        }
      })

    }

  },

  mapView: function () {
    var th = this;
    wx.chooseLocation({
      success: function (res) {
        var s1 = res.longitude + "";
        var s2 = res.latitude + "";
        var str1 = s1.substring(0, s1.indexOf(".") + 7);
        var str2 = s2.substring(0, s2.indexOf(".") + 7);
        th.setData({
          longitude: str1,
          latitude: str2,
          address: res.address
        })

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  //打开省市区弹出框
  openCitySelect: function() {
    var th = this;
    th.setData({
      citySelectShow: true
    });

  },
  //点击遮罩层关闭省市区弹出框
  onCloseCitySelect: function() {
    var th = this;
    th.setData({
      citySelectShow: false
    });
  },
  citySelectCancel:function(){
    var th = this;
    th.setData({
      citySelectShow: false
    });
  },
  citySelectConfirm:function(){
    var th = this;
    th.setData({
      citySelectShow: false,
      displayArea: res.detail.values[0].name + '->' + res.detail.values[1].name + '->' + res.detail.values[2].name,
      province: res.detail.values[0].code,
      city: res.detail.values[1].code,
      area: res.detail.values[2].code
    });
  },
  //点击确认关闭省市区弹出框
  citySelectConfirm: function(res) {
    var th = this;
    th.setData({
      citySelectShow: false,
      displayArea: res.detail.values[0].name + '->' + res.detail.values[1].name + '->' + res.detail.values[2].name,
      province: res.detail.values[0].code,
      city: res.detail.values[1].code,
      area: res.detail.values[2].code
    });

  },
  onAddressChange(event) {
    let _that = this;
    _that.setData({
      address: event.detail
    });
  },
  /**
   * 删除
   */
  deleteRelation: function(option) {
    let _that = this;
    let id = _that.data.id;
    let uuid = _that.data.uuid;
    wx.request({
      url: url + '/CommonRelationApi/deleteById',
      data: {
        id: id,
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(uuid);
          wx.showToast({
            title: "删除成功!",
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },
  /**表单 end */
  openRelationPop: function(option) {
    let _that = this;
    let type = option.currentTarget.dataset.type;
    if (type == 1) {
      let name = option.currentTarget.dataset.name;
      let phone = option.currentTarget.dataset.phone;
      let status = option.currentTarget.dataset.defaultstatus;
      let id = option.currentTarget.dataset.id;
      let address = option.currentTarget.dataset.address;
      let defaultStatus;
      if (status == 0) {
        defaultStatus = false
      } else {
        defaultStatus = true
      }
      _that.setData({
        id: id,
        userName: name,
        phone: phone,
        checked: defaultStatus,
        address: address
      })
    }
    _that.setData({
      btnType: type,
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onLoad: function(options) {
    let _that = this;
    wx.getStorage({
      key: "openId",
      success(res) {
        console.log(res);
        _that.setData({
          uuid: res.data
        })
        _that.queryData(res.data);
      },
      fail(res){
        // 尚未登录。弹出登录框
        Dialog.confirm({
          title: '提示',
          message: '您当前尚未登录, 请点击确认跳转登录页面!'
        }).then(() => {
          // on confirm
          wx.navigateTo({
            url: "../../pages/login/index"
          })
        }).catch(() => {
          // on cancel
        });
      }
    })
    //省市区选择赋值
    _that.setData({
      areaList: areasInfo
    });
  },
  queryData: function(uuid) {
    let _that = this;
    wx.request({
      url: url + '/CommonRelationApi/listPageAll',
      data: {
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            relationList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },
  changeVal: function(e) {
    let _that = this;
    _that.setData({
      value: e.detail
    })
  },
  submit: function(e) {
    let _that = this;
    let mark = _that.data.value;
    console.log(mark);
    let orderId = _that.data.orderId;
    wx.request({
      url: url + '/OrderApi/updateMarkByOrderId',
      data: {
        orderId: orderId,
        mark: mark
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '保存备注失败!',
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  }


})